package com.test.daily.leetcode.y2022.m07.day0705.v05;

import java.util.LinkedList;
import java.util.Queue;

/**
 * @descriptions:
 * @author: Tom
 * @date: 2022/7/5 下午 06:10
 * @version: 1.0
 */
public class Solution {
    public static class QueueImplementStack{
        public Queue<Integer> queue;
        public Queue<Integer> help;
        public QueueImplementStack(){
            this.queue = new LinkedList<>();
            this.help = new LinkedList<>();
        }
        public void push(int data){
            this.queue.offer(data);
        }
        public  int pop(){
            if (this.queue.isEmpty()){
                throw new RuntimeException("stack is empty");
            }
            while(queue.size() > 1){
                this.help.offer(queue.poll());
            }
            int ans = queue.poll();
            Queue tmp = queue;
            this.queue = this.help;
            this.help = this.queue;
            return ans;
        }
        public int peek(){
            if(this.queue.size() == 0){
                throw new RuntimeException("stack is empty.");
            }
            while(this.queue.size() > 1){
                this.help.offer(this.queue.poll());
            }
            int ans = this.queue.poll();
            this.help.offer(ans);
            Queue tmp = this.queue;
            this.queue = this.help;
            this.help = tmp;
            return ans;
        }
    }
}
